//================DS1302================
#define DS1302_Second	0x80
#define DS1302_Minute	0x82
#define DS1302_Hour		0x84
#define DS1302_Date		0x86
#define DS1302_Month 	0x88
#define DS1302_Day 		0x8A
#define DS1302_Year 	0x8C
#define DS1302_WP	 	0x8E

volatile unsigned char DS1302_Read_Time[3] = {0,0,0};
volatile unsigned char DS1302_Data_M=0;
volatile unsigned char DS1302_Data_H=0;
volatile unsigned int DS1302_Data_ALL=0;

void DS1302_Write(uchar Data);
void DS1302_Write_Byte(uchar Command,uchar Data);
unsigned char DS1302_Read_Byte(uchar Command);
void DS1302_WriteTime_Handle(uchar Hour,uchar Minute);
void DS1302_ReadTime_Handle();

void DS1302_Write(uchar Data)
{
	uchar i;
	for(i=0; i<8; i++)
	{
		DS1302_CLK_LOW;
		DS1302_DIO = Data&0x01;
		Data >>= 1;
		DS1302_CLK_HIGH;
	}
}

void DS1302_Write_Byte(uchar Command,uchar Data)
{
	DS1302_DIO_OUT();
	DS1302_CE_LOW;DelayUs(2);
	DS1302_CLK_LOW;DelayUs(2);
	DS1302_CE_HIGH;DelayUs(2);
	DS1302_Write(Command);
	DS1302_Write(Data);
	DS1302_CE_LOW;
}

unsigned char DS1302_Read_Byte(uchar Command)
{
	uchar i,Data=0;
	Command |= 0x01;
	DS1302_CE_LOW;DelayUs(2);
	DS1302_CLK_LOW;DelayUs(2);
	DS1302_CE_HIGH;DelayUs(2);
	DS1302_DIO_OUT();
	DS1302_Write(Command);
	DS1302_DIO_IN();
	for(i=0; i<8; i++)
	{
		DS1302_CLK_LOW;
		Data >>= 1;
		if(DS1302_DIO) Data |= 0x80;
		DS1302_CLK_HIGH;
	}

	DS1302_CE_LOW;DelayUs(2);
	DS1302_CLK_LOW;DelayUs(2);
	DS1302_CLK_HIGH;DelayUs(2);
	DS1302_DIO = 0;DelayUs(2);
	DS1302_DIO = 1;DelayUs(2);
	return Data;
}

void DS1302_WriteTime_Handle(uchar Hour,uchar Minute)
{
	DS1302_Write_Byte(DS1302_WP,0x00);
	DS1302_Write_Byte(DS1302_Hour,Hour);
	DS1302_Write_Byte(DS1302_Minute,Minute);
    DS1302_Write_Byte(DS1302_Second,0);
	DS1302_Write_Byte(DS1302_WP,0x80);
	
	T2_1s_IF = 0;T2_1s = 0;
}

void DS1302_ReadTime_Handle()
{
    DS1302_Read_Time[0] = DS1302_Read_Byte(DS1302_Hour);
    DS1302_Read_Time[1] = DS1302_Read_Byte(DS1302_Minute);
    DS1302_Read_Time[2] = DS1302_Read_Byte(DS1302_Second);
}